MZList = function(options){
	return this.init(options);
}


MZList.fn = MZList.prototype = {
    id: null,
    tableObj: null,
	_events : {     //绑定事件
        operation: []
    },

}


MZList.fn.init = function(options){
    var that = this;

    if(layui.$(options.elem).length <= 0){
        console.warn('list elem not exists !');
        return;
    }

    that.id = layui.$(options.elem).attr("id");

    layui.$(options.elem).attr("lay-filter", that.id);

    var config = {
        title: '用户表'
        ,page: true //开启分页
        ,request: {
            pageName: 'page',
            limitName: 'size'
        }
        ,response: {
            statusCode: 200,
            msgName: 'msg',
            countName: 'total',
            dataName: 'data'
        }
        ,toolbarEvents: {    //行事件处理(ajax)

        }
    }

    Object.assign(config, options);

    that.config = config;

    //加载模块  
    layui.use(['table'], function(){
        //得到各种内置组件
        var table = layui.table //表格
        
        //执行一个 table 实例
        table.render(config);

         //监听行工具事件
        table.on('tool(' + that.id + ')', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            that._call_event('operation', obj);
            var eventParams =  that.config.toolbarEvents[obj.event];
            if(eventParams && eventParams.url){
                if(typeof that['_'+obj.event+'_item'] == 'function'){
                    that['_'+obj.event+'_item'](eventParams.url, obj.data);
                }else{
                    console.warn('list operation %s not support !', obj.event);
                }
            }
            else if(typeof eventParams == 'function'){
                eventParams(obj.data);
            }

        });
    });

}

MZList.fn.reload = function(params){
    layui.table.reload(this.id, params);
}

MZList.fn.bind = function(type, fun){
	var that = this;
	if(that._events[type]){
		that._events[type].push(fun);
	}
	return that;
}


MZList.fn._call_event = function(type, params){
	var that = this;
	var evt = that._events[type];
	if(evt){
		for(var i in evt){
			evt[i](params);
		}
	}
	return that;
}


MZList.fn._view_item = function(url, data){
    url = url.indexOf('?') >= 0 ? (url + '&id=' + data.id) : url + '?id=' + data.id;
    location.href = url;
}



MZList.fn._edit_item = function(url, data){
    url = url.indexOf('?') >= 0 ? (url + '&id=' + data.id) : url + '?id=' + data.id;
    location.href = url;
}


MZList.fn._remove_item = function(url, data){
    var that = this;

    layer.confirm('确认删除?', {icon: 3, title:'ALERT'}, function(index){
        layer.close(index);
        layui.$.ajax({
            url: url,
            type: "delete",
            contentType: "application/json",
            data: JSON.stringify({id: data.id}),
            dataType: "json",
            async: false,
            processData: false,
            success: function (res) {
                if(res.content){
                    layer.msg('提交成功', function () {
                        layui.table.reload(that.id);
                    });
                }else{
                    layer.msg('提交失败');
                }
            }
        });
    });


}


